/*
 * @Author: yangni 920556271@qq.com
 * @Date: 2023-09-01 15:20:41
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2024-04-24 09:05:17
 * @FilePath: /yt-devops/lib/pages/devices/device_tab/device_detail/battery_pack_detail/battery_pack_detail_controller.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yt_devops/pages/devices/device_tab/device_detail/base_detail_controller/base_detail_controller.dart';
import 'package:yt_devops/pages/devices/device_tab/device_detail/battery_cell_tree/battery_cell_tree_controller.dart';
import 'package:yt_devops/pages/devices/device_tab/device_detail/battery_cell_tree/battery_cell_tree_model.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/routers/router_path.dart';
import 'package:yt_devops/utils/enums.dart';

import 'components/battery_cell_widget.dart';

class BatteryPackDetailController extends BaseDetailController
    with GetSingleTickerProviderStateMixin {
  BatteryCellTreeController batteryCellTreeController =
      Get.put(BatteryCellTreeController());

  List<BadgeTab> tabs = [
    BadgeTab(text: ResKey.deviceBatteryStackInformation.tr),
    BadgeTab(text: ResKey.deviceBatteryClusterInformation.tr),
  ];

  late TabController tabController;

  /// 设备名称
  String title = '';

  /// 设备id
  late int deviceId;

  /// product
  late int productId;

  /// 当前展示的电池簇id
  BatteryCellTreeModel? selectedBatteryCell;

  GlobalKey<BatteryCellWidgetState> cellKey =
      GlobalKey<BatteryCellWidgetState>();

  List<int> remoteSettinType = [
    ProductIDType.yTEnergyBatteryStack.value,
    ProductIDType.liquidEnergyBatteryStack.value
  ];

  bool get hasRemoteSetting => remoteSettinType.contains(productId);

  @override
  void onInit() {
    deviceId = Get.arguments['deviceId'];
    title = Get.arguments['deviceName'];
    connectStatus = Get.arguments['connectStatus'];
    productId = Get.arguments['productId'];
    super.onInit();
    tabController =
        TabController(length: tabs.length, vsync: this, initialIndex: 0);

    tabController.addListener(() {
      if (tabController.previousIndex == 1) {
        /// 关掉 bmu 的 tab 弹出窗
        cellKey.currentState?.closeController.closeMoreWindow();
      } else if (tabController.index == 1) {
        changeTab(tabController.index);
      }
    });

    batteryCellTreeController.getBatteryCell();
  }

  @override
  void onReady() {}

  @override
  void onClose() {
    tabController.dispose();
  }

  /// 切换到电池簇时， 判断是否多簇，多簇则跳到选择簇页面， 单簇则直接显示
  changeTab(int index) async {
    if (index == 1) {
      if (batteryCellTreeController.cells.isEmpty) {
        await batteryCellTreeController.getBatteryCell();
      }
      if (batteryCellTreeController.cells.length == 1) {
        selectedBatteryCell = batteryCellTreeController.cells[0];
        update();
      } else {
        Get.toNamed(RouterPath.batteryCellTree, arguments: {
          'deviceId': deviceId,
          'deviceName': title,
          'model': selectedBatteryCell
        })?.then((value) {
          if (value is BatteryCellTreeModel) {
            selectedBatteryCell = value;
            update();
          } else {
            tabController.animateTo(0);
          }
        });
      }
    }
  }
}
